//
//  BSVideoView.m
//  Baisijie
//
//  Created by 王建伟 on 2017/3/24.
//  Copyright © 2017年 RangGuangyu. All rights reserved.
//

#import "BSVideoView.h"
#import "BSVideoItem.h"
#import <UIImageView+WebCache.h>
#import "BSDownloadImageTool.h"

@interface BSVideoView ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *playCountLbl;
@property (weak, nonatomic) IBOutlet UILabel *playTimeLbl;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewW;

@end

@implementation BSVideoView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.autoresizingMask = UIViewAutoresizingNone;
    [_imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture)]];
}
- (void)tapGesture {
    [self videoBtnClick];
}
- (IBAction)videoBtnClick {
    BSLogFunc
}
- (void)setVideo:(BSVideoItem *)video {
    _video = video;
    CGFloat cellW = KScreenW - 2 * BSMargin;
    CGFloat videoH = cellW * video.height / video.width;
    if (videoH > cellW) {
        _imageViewW.constant = - cellW * (1 - video.width / video.height);
    } else {
        _imageViewW.constant = 0;
    }
    [BSDownloadImageTool bs_showImageInImageView:_imageView bigImageUrl:video.thumbnail.firstObject thumbnailImageUrl:video.thumbnail.firstObject useTop:NO progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        _imageView.userInteractionEnabled = image;
    }];
    _playCountLbl.text = [NSString stringWithFormat:@"%zd播放",video.playcount];
    NSString *timeStr = @"";
    if (video.duration < 60) {
        timeStr = [NSString stringWithFormat:@"00:%02zd",video.duration];
    } else {
        timeStr = [NSString stringWithFormat:@"%02zd:%02zd",video.duration / 60,video.duration % 60];
    }
    _playTimeLbl.text = timeStr;
}

@end
